import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/assets_tool/app_colors.dart';
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/tools_base/global_store/store.dart';

import '../../../alert_business/video/video_buy_bottom_sheet.dart';
import '../../../hj_model/video_model.dart';
import '../../home_business/widget/image_scale_view.dart';

class CommunityImagePage extends StatefulWidget {
  final VideoModel? videoModel;
  final int? index;
  final List<String>? imageList;
  const CommunityImagePage({
    Key? key,
    this.videoModel,
    this.index,
    this.imageList,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _CommunityImagePageState();
  }
}

class _CommunityImagePageState extends State<CommunityImagePage> {
  late PageController pageController;
  int currentIndex = 0;

  VideoModel? get videoModel => widget.videoModel;

  int get itemCount => widget.imageList?.length ?? videoModel?.seriesCover?.length ?? 0;

  int get maxCount {
    if (widget.imageList?.isNotEmpty == true) {
      return widget.imageList?.length ?? 0;
    }
    int len = videoModel?.seriesCover?.length ?? 0;
    if (len > 6) len = 6;
    return len;
  }

  bool isShowBuyAlert(int index) {
    if (widget.imageList?.isNotEmpty == true) {
      return false;
    }
    if (widget.videoModel?.isVideo() == true) {
      return false;
    }
    if (widget.videoModel?.isSeedPost() == true) {
      return false;
    }
    if (widget.videoModel?.permission == 2) {
      return false;
    }
    if (!globalStore.isVIP() && widget.videoModel?.vidStatus?.hasPaid != true && index == 2) {
      return true;
    }
    return false;
  }

  void _showBuyEvent(BuildContext context) async {
    if (widget.imageList?.isNotEmpty == true) {
      return;
    }
    if (videoModel?.permission == 0) {
      await Get.bottomSheet(VideoBuyBottomSheet(videoM: videoModel!));
    } else if (widget.videoModel?.permission == 1) {
      final res = await Get.bottomSheet(VideoBuyBottomSheet(videoM: videoModel!));
      if (res == true) {
        widget.videoModel?.vidStatus?.hasPaid = true;
        setState(() {});
      }
    } else {}

    setState(() {});
  }

  @override
  void initState() {
    super.initState();
    currentIndex = widget.index ?? 0;
    pageController = PageController(initialPage: currentIndex);
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.black,
      child: Stack(
        fit: StackFit.expand,
        children: [
          PageView.builder(
            itemCount: itemCount,
            controller: pageController,
            itemBuilder: (context, index) {
              if (widget.imageList?.isNotEmpty == true) {
                return ImageScaleView(
                  url: widget.imageList?[index],
                );
              } else {
                return ImageScaleView(url: widget.videoModel?.seriesCover?[index]);
              }
            },
            onPageChanged: (index) {
              if (isShowBuyAlert(index)) {
                _showBuyEvent(context);
                pageController.jumpToPage(max(0, index - 1));
              } else {
                currentIndex = index;
                setState(() {});
              }
            },
          ),
          Positioned(
              bottom: 50,
              right: 20,
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 11, vertical: 2),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(20),
                ),
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text(
                      "${currentIndex + 1}",
                      style: TextStyle(color: AppColors.actionRed, fontSize: 12),
                    ),
                    Text(
                      "/$itemCount",
                      style: TextStyle(color: Color(0xff3D3D3D), fontSize: 12),
                    ),
                  ],
                ),
              )),
          Positioned(
            top: 0,
            left: 16,
            child: SafeArea(
              child: InkWell(
                onTap: Get.back,
                child: Image.asset('back_circle.png'.commonImgPath, width: 24),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
